package com.finace.money.ui.activity;

import android.app.AlertDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.support.v4.content.LocalBroadcastManager;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.finace.money.R;
import com.finace.money.common.Constant;
import com.finace.money.common.util.MessageSendUtil;
import com.finace.money.common.util.StringUtil;
import com.finace.money.common.util.UIUtil;
import com.finace.money.data.bean.CashOperationBean;
import com.finace.money.di.component.AppComponent;
import com.finace.money.interfaces.OnItemClickListener;
import com.finace.money.net.socket.PushReceiver;
import com.finace.money.ui.adapter.MoneyInMoneyAdapter;
import com.unionpay.UPPayAssistEx;
import com.unionpay.uppay.PayActivity;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Created by zhangpeng on 2017/11/28.
 */

public class MoneyInActivity extends BaseActivity implements View.OnClickListener {
    @BindView(R.id.rl_left)
    RelativeLayout rlLeft;
    @BindView(R.id.tv_head_title)
    TextView tvHeadTitle;
    @BindView(R.id.tv_right_title)
    TextView tvRightTitle;
    @BindView(R.id.et_money)
    EditText etMoney;
    @BindView(R.id.recycle_money)
    RecyclerView recycleMoney;
    @BindView(R.id.button_pay)
    Button buttonPay;

    private List<String> moneys = new ArrayList<>();

    @Override
    public int setLayout() {
        return R.layout.activity_money_in;
    }

    @Override
    public void init() {
        tvRightTitle.setVisibility(View.VISIBLE);
        tvHeadTitle.setText(UIUtil.getString(R.string.money_in));
        tvRightTitle.setText(UIUtil.getString(R.string.money_in_record));
        rlLeft.setOnClickListener(this);
        tvRightTitle.setOnClickListener(this);
        buttonPay.setOnClickListener(this);
        moneys.add("2000");
        moneys.add("5000");
        moneys.add("10000");

        GridLayoutManager gridLayoutManager = new GridLayoutManager(UIUtil.getContext(), 3,GridLayoutManager.VERTICAL, false);
        recycleMoney.setLayoutManager(gridLayoutManager);
        MoneyInMoneyAdapter moneyInMoneyAdapter = new MoneyInMoneyAdapter();
        moneyInMoneyAdapter.setmDatas(moneys);
        recycleMoney.setAdapter(moneyInMoneyAdapter);
        moneyInMoneyAdapter.setmListener(new OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                etMoney.setText(moneys.get(position));
            }
        });
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(PushReceiver.ACTION_CASH_OPERATION);
        LocalBroadcastManager.getInstance(UIUtil.getContext()).registerReceiver(broadcastReceiver, intentFilter);
        unregisterReceiver(broadcastReceiver);
    }

    private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {

            if(null != intent){
                CashOperationBean cashOperationBean = (CashOperationBean) intent.getSerializableExtra(PushReceiver.ACTION_CASH_OPERATION);
                if(null != cashOperationBean){
                    showLoading(false);
                    //跳转银联
                    String serverMode = "01";
                    String tn=cashOperationBean.getTN();
                    UPPayAssistEx.startPayByJAR(MoneyInActivity.this,
                            PayActivity.class, "", "", tn, serverMode);
                }
            }
        }
    };

    @Override
    public void setupAcitivtyComponent(AppComponent appComponent) {

    }


    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.rl_left:
                onBackPressed();
                break;
            case R.id.tv_right_title:
                Intent intent = new Intent(this, MoneyRecordActivity.class);
                intent.putExtra(Constant.MONEY_RECORD,Constant.MONEY_IN);
                startActivity(intent);
                break;
            case R.id.button_pay:
                String amount = etMoney.getText().toString();
                if(StringUtil.isNullString(amount)){
                    UIUtil.showToastSafe(getString(R.string.money_rechange_input_please));
                    return;
                }
                MessageSendUtil.CashOperationRequest(amount,"Deposit");
                showLoading(true);
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        /*************************************************
         * 步骤3：处理银联手机支付控件返回的支付结果
         ************************************************/
        if (data == null) {
            return;
        }

        String msg = "";
        /*
         * 支付控件返回字符串:success、fail、cancel 分别代表支付成功，支付失败，支付取消
         */
        String str = data.getExtras().getString("pay_result");
        if (str.equalsIgnoreCase("success")) {

            // 结果result_data为成功时，去商户后台查询一下再展示成功
            msg = getString(R.string.pay_sucess);
        } else if (str.equalsIgnoreCase("fail")) {
            msg = getString(R.string.pay_fail);
        } else if (str.equalsIgnoreCase("cancel")) {
            msg = getString(R.string.pay_cancel);
        }

        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle(R.string.pay_reslut_push);
        builder.setMessage(msg);
        builder.setInverseBackgroundForced(true);
        // builder.setCustomTitle();
        builder.setNegativeButton(getString(R.string.sure), new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });
        builder.create().show();
    }


}
